package com.example.administrator.printoutindemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.administrator.printoutindemo.CangKuActivity;
import com.example.administrator.printoutindemo.R;
import com.example.administrator.printoutindemo.bean.InKuBean;
import com.example.administrator.printoutindemo.http.SPUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/9/10.
 */

public class InKuAdapter extends BaseAdapter {

    Context context;
    List<InKuBean> mlists = new ArrayList<>();
    LayoutInflater inflater;

    public InKuAdapter(Context context, List<InKuBean> mlists) {
        this.context = context;
        this.mlists = mlists;
        this.inflater = LayoutInflater.from(context);
    }

    public void shua() {
        if (mlists == null)
            mlists = new ArrayList<>();
        notifyDataSetInvalidated();
    }

    @Override
    public int getCount() {
        return mlists.size();
    }

    @Override
    public Object getItem(int i) {
        return mlists.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            Log.e("hh====================", "11111");
            view = getInflater().inflate(R.layout.item_chaxun, null);
            holder = new ViewHolder();
            holder.tv_xmmc = view.findViewById(R.id.tv_xmmc);
            holder.tv_gys = view.findViewById(R.id.tv_gys);
            holder.tv_wlmc = view.findViewById(R.id.tv_wlmc);
            holder.tv_shuliang = view.findViewById(R.id.tv_shuliang);
            holder.tv_ddh = view.findViewById(R.id.tv_ddh);
            holder.tv_cwxz = view.findViewById(R.id.tv_cwxz);
            holder.tv_ji_hua_dao_huo_shi_jian = view.findViewById(R.id.tv_ji_hua_dao_huo_shi_jian);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        if (mlists.get(i).getPurchaseid() != null) {
            holder.tv_ddh.setText(mlists.get(i).getPurchaseid());
        }
        if (mlists.get(i).getProjectName() != null) {
            holder.tv_xmmc.setText(mlists.get(i).getProjectName());
        }
        if (mlists.get(i).getMaterial() != null) {
            holder.tv_wlmc.setText(mlists.get(i).getMaterial());
        }
        if (String.valueOf(mlists.get(i).getDnum()) != null) {
            holder.tv_shuliang.setText(String.valueOf(mlists.get(i).getDnum()));
        }
        if (mlists.get(i).getSname() != null) {
            holder.tv_gys.setText(mlists.get(i).getSname());
        }
        //计划到货时间
        if (mlists.get(i).getSupplyDateStatusDate() != null) {
            holder.tv_ji_hua_dao_huo_shi_jian.setText(mlists.get(i).getSupplyDateStatusDate());
        }
        holder.tv_cwxz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SPUtil.putInt(context, "dids", mlists.get(i).getDid());
                SPUtil.putString(context, "shuliang", String.valueOf(mlists.get(i).getDnum()));
//                SPUtil.putString(context, "xiangMuMingCheng", String.valueOf(mlists.get(i).getProjectName()));
//                SPUtil.putString(context, "gongYingShang", String.valueOf(mlists.get(i).getSname()));
//                SPUtil.putString(context, "dingDanHao", String.valueOf(mlists.get(i).getPurchaseid()));
//                SPUtil.putString(context, "wuLiaoMingCheng", String.valueOf(mlists.get(i).getMaterial()));

                Bundle bundle = new Bundle();
                bundle.putString("xiangMuMingCheng", String.valueOf(mlists.get(i).getProjectName()));
                bundle.putString("gongYingShang", String.valueOf(mlists.get(i).getSname()));
                bundle.putString("dingDanHao", String.valueOf(mlists.get(i).getPurchaseid()));
                bundle.putString("wuLiaoMingCheng", String.valueOf(mlists.get(i).getMaterial()));
                bundle.putString("shuliang", String.valueOf(mlists.get(i).getDnum()));
                bundle.putString("isqualityplan", String.valueOf(mlists.get(i).getIsqualityplan()));
                Intent intent = new Intent(context, CangKuActivity.class);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });

        return view;
    }

    public LayoutInflater getInflater() {
        return inflater;
    }

    public void setInflater(LayoutInflater inflater) {
        this.inflater = inflater;
    }

    public void addAll(List<InKuBean> sdc) {
        mlists.addAll(sdc);
        notifyDataSetChanged();
    }

    public void clearData() {
        mlists.clear();
    }

    class ViewHolder {
        TextView tv_xmmc, tv_gys, tv_wlmc, tv_shuliang, tv_ddh, tv_cwxz,tv_ji_hua_dao_huo_shi_jian;
    }
}
